package com.example.wanbaqian.activity;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.example.wanbaqian.R;
import com.example.wanbaqian.adapter.RepaymentListAdapter;
import com.example.wanbaqian.base.BaseActivity;
import com.example.wanbaqian.config.CommonValue;
import com.example.wanbaqian.base.HttpCallback;
import com.example.wanbaqian.config.HttpURL;
import com.example.wanbaqian.base.RefreshCallback;
import com.example.wanbaqian.bean.Info;
import com.example.wanbaqian.bean.RepaymentListBean;
import com.example.wanbaqian.utils.GetJsonUtils;
import com.example.wanbaqian.utils.HttpUtils;
import com.example.wanbaqian.utils.JsonUtils;
import com.example.wanbaqian.utils.SPUtils;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.wrapper.EmptyWrapper;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 所有借款的还款列表
 */
public class RepaymentListActivity extends BaseActivity implements MultiItemTypeAdapter.OnItemClickListener, HttpCallback, RefreshCallback {
    @BindView(R.id.RepaymentList_RecyclerView)
    RecyclerView RepaymentListRecyclerView;
    private RepaymentListAdapter adapter;
    private List<RepaymentListBean> list = new ArrayList<>();
    private EmptyWrapper emptyWrapper;

    @Override
    protected int getLayout() {
        return R.layout.activity_repayment_list;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        setStatusBarColor(R.color.colorBackground);
        setToolBar("还款列表");
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        setHeaderAndFooter(R.id.RepaymentList_SpringView, true, true, this);
        RepaymentListRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        adapter = new RepaymentListAdapter(mContext, R.layout.item_repayment_list, list);
        emptyWrapper = new EmptyWrapper(adapter);

        View emptyView = getLayoutInflater().inflate(R.layout.view_empty_activits, RepaymentListRecyclerView, false);
        TextView emptyTv = (TextView) emptyView.findViewById(R.id.emptyTv);
        emptyTv.setText("您暂无账单，马上提取一笔吧~");
        emptyTv.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(mContext, R.drawable.empty_money), null, null);
        emptyWrapper.setEmptyView(emptyView);
        RepaymentListRecyclerView.setAdapter(emptyWrapper);
        adapter.setOnItemClickListener(this);
    }

    @Override
    protected void initDate() {
        HttpUtils.getInstance().post(GetJsonUtils.getInstance().getRequest(HttpURL.LOAN_LIST, mContext,
                new String[]{"accessid", "cur_page", "page_size"},
                new String[]{SPUtils.getAccessid(mContext), page + "", count + ""}), mContext, HttpURL.LOAN_LIST, CommonValue.REQUEST_CODE1, this);
    }

    @Override
    public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
        Bundle bundle = new Bundle();
        bundle.putString("loan_no", list.get(position).getLoan_no());
        openActivity(RepaymentListByStageActivity.class, bundle);
    }

    @Override
    public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
        return false;
    }
    @Override
    public void onSuccess(String content, Info info, int requestCode) {
        springView.onFinishFreshAndLoad();
        if (page == 1) {
            list.clear();
        }
        list.addAll(JsonUtils.getInstance().getList(content, "loan_list", RepaymentListBean.class));
        emptyWrapper.notifyDataSetChanged();
        canLoadmore = JsonUtils.getInstance().havePage(content);
    }
    @Override
    public void onCacheSuccess(String content, Info info, int requestCode) {

    }
    @Override
    public void onError(Info info, int requestCode) {
        showToast(info.getMsg());
    }

    @Override
    public void getNetDate() {
        initDate();
    }
}
